////// EzMemberSelector
(function ($, undefined) {

    // define the name of the UI component
    var ui_name = "memberSelector"

    // initialize object
    var ms = $.memberSelector = {

        // default values
        defaults: {
            deptList: []
        },

        setDefaults: function (defaultOptions) {
            this.defaults = $.extend({}, this.defaults, defaultOptions);
        }

    };

    $.fn.memberSelector = function (options) {

        var _cmbDeptList = null;
        var _cmbMemberList = null;
        var _selectUserId = null;
        var _domInitialized = false;


        // method calling
        if (typeof options == 'string') {
            var args = Array.prototype.slice.call(arguments, 1),
			    res;
            this.each(function () {
                var data = $.data(this, ui_name);
                if (data) {
                    var meth = data[options];
                    if (meth) {
                        var r = meth.apply(this, args);
                        if (res === undefined) {
                            res = r;
                        }
                    }
                }
            });
            if (res !== undefined) {
                return res;
            }
            return this;
        }

        // initialize options
        options = $.extend({}, ms.defaults, options);

        this.each(function () {
            var _self = this;

            if (_self) {
                var dom = getInitializeDom(options);
                $(this).append(dom);
            }

            // initialize callback
            for (var key in options) {
                var value = options[key];
                if (typeof value == 'function') {
                    // bind callbacks
                    $(_self).bind(key, value);
                }
            }

            /* Private Methods
            -----------------------------------------------------------------------------*/
            function _accessor() {

                var accessor = {};

                if (ezmock.IS_MOCKED) {
                    var mock_memberList = [
                       {
                           'Id': '11111',
                           'Name': '李6'
                       },
                       {
                           'Id': '22222',
                           'Name': '李7'
                       },
                       {
                           'Id': '33333',
                           'Name': '李8'
                       }
                     ];

                    var mock_deptList = [
                        {
                            'Id': '111',
                            'DeptName': '软件工程'
                        },
                        {
                            'Id': '222',
                            'DeptName': '策划'
                        },
                        {
                            'Id': '333',
                            'DeptName': '设计'
                        }
                    ];

                    var mockAccessor = {
                        'getDeptList': function () {
                             return  mock_deptList;
                        },

                        'doUpdateMemberList': function () {
                            _bindMemberList(mock_memberList);

                        }
                    }
                    accessor = mockAccessor;
                }
                else {
                    var realAccessor = {
                        'getDeptList': function () {
                            return options.deptList;
                        },

                        'doUpdateMemberList': function (deptId, userId) {
                            $(document).ajaxBusy('begin');
                            $.getJSON(URL_SIMPLEUSERINFO_BY_DEPTID,
                                { 'deptId': deptId },
                                function (e) {
                                    $(document).ajaxBusy('end');
                                    _bindMemberList(e, userId);

                                });
                        },
                        
                    }
                    accessor = realAccessor;
                }
                return accessor;
            };

            //将Json格式的部门列表转换成数组
            function _getDeptListForArray(deptList) {
                var deptListForArray = [];
                for (var key in deptList) {
                    var name = deptList[key].DeptName;
                    var id = deptList[key].Id;

                    deptListForArray.push({ 'text': name,
                        'value': id
                    });
                }
                return deptListForArray;
            };


            //绑定部门列表
            function _bindDeptList(deptList) {

                var deptListForArray = _getDeptListForArray(deptList);

                _cmbDeptList
                  .comboxBinding(deptListForArray, 'text', 'value');

//                if (_selectedDeptId) {
//                    _cmbDeptList.val(_selectedDeptId);
//                }
            };


            //将Json格式的该部门的成员列表转换成数组
            function _getMemberListForArray(memberList) {

                var memberListForArray = [];
                for (var key in memberList) {
                    var name = memberList[key].Name;
                    var id = memberList[key].Id;

                    memberListForArray.push({ 'text': name,
                        'value': id
                    });
                }
                return memberListForArray;
            };


            //绑定成员列表
            function _bindMemberList(memberList, userId) {

                var memberDropDownList = _getMemberListForArray(memberList);

                _cmbMemberList
                    .comboxBinding(memberDropDownList, 'text', 'value');

                if (userId) {
                    _cmbMemberList.val(userId);
                }

                memberChanged(_cmbMemberList.val());
                

            };

            //获取当前员工信息
            function _getCurretnUser(options){
                if(typeof options.currentUser == 'string')  {
                    //todo..  
                    return options.currentUser;
                }
                else {
                    return options.currentUser;
                
                }
            
            };


            //init memberSelector
            function getInitializeDom(optins) {
                //初始化DOM
                var domRoot = $("<div/>");
                domRoot.append($('<span>部门:</span>'));

                _cmbDeptList =
                    $('<select class="cmbDepartmentList" />')
                        .css({ 'width': '100px' });
                domRoot.append(_cmbDeptList);

                domRoot.append($('<span>姓名:</span>'));

                _cmbMemberList =
                    $('<select class="cmbMemberList" />')
                        .css({ 'width': '80px' });
                domRoot.append(_cmbMemberList);

                //获取并绑定部门列表
                var deptList = _accessor().getDeptList();
                _bindDeptList(deptList);
                //当前员工
                var currentUser = _getCurretnUser(options);
                _accessor().doUpdateMemberList(currentUser.Dept.Id, currentUser.Id);
                _cmbDeptList.val(currentUser.Dept.Id);
                
               
                _cmbDeptList.change(function () {
                    var deptId = _cmbDeptList.val();
                    _accessor().doUpdateMemberList(deptId);
                   
                });

                //memberChanged callback
                _cmbMemberList.change(function () {
                    var userId = _cmbMemberList.val();
                    memberChanged(userId);
                });


                return domRoot;
            };

            /* Callback Triggers
            -----------------------------------------------------------------------------*/

            function memberChanged(id) {
                $(_self).trigger('memberChanged', id);
            };






            /* Public Methods
            -----------------------------------------------------------------------------*/

            var publicMethods = {
                'option': function (name, value) {
                    if (value === undefined) {
                        return options[name];
                    }
                    options[name] = value;
                },

                //获得部门信息
                'getDept': function () {
                    var dept = {};
                    dept.Id = _cmbMemberList.val();
                    dept.DeptName = _cmbDeptList.find("option:selected").text();
                    return dept;
                },


                //获得成员信息
                'getMember': function () {
                    var member = {};
                    member.Id = _cmbMemberList.val();
                    member.Name = _cmbMemberList.find("option:selected").text();
                    return member;
                }

                // @other public methods
            };

            $.data(this, ui_name, publicMethods);


            // @private methods


        });

        return this;
    };

    // @static methods

})(jQuery);

//var EzMemberSelector = function (selectorId) {
//    this._selectorId = selectorId;
//    this._init();
//};

//EzMemberSelector.prototype = {
//    '_init': function () {
//    },

//    '_accessor': function () {
//        var _self = this;
//        var accessor = {};

//        if (ezmock.IS_MOCKED) {
//            var mock_memberList = [
//               {
//                   'Id': '11111',
//                   'Name': '李6'
//               },
//               {
//                   'Id': '22222',
//                   'Name': '李7'
//               },
//               {
//                   'Id': '33333',
//                   'Name': '李8'
//               }
//             ];
//            var mockAccessor = {
//                'getDeptList': function () {
//                    return mock_deptList;
//                },
//                'doUpdateMemberList': function () {
//                    _self._memberInitList = _self._getInitMemberList(mock_memberList);
//                    _self._bindMemberList();
//                }
//            }
//            accessor = mockAccessor;
//        }
//        else {
//            var realAccessor = {
//                'doUpdateMemberList': function (deptId) {
//                    $.ajaxBusy('begin');
//                    $.getJSON(URL_SIMPLEUSERINFO_BY_DEPTID,
//                        { 'deptId': deptId },
//                        function (e) {
//                            $.ajaxBusy('end');

//                            _self._memberInitList = _self._getInitMemberList(e);
//                            _self._bindMemberList();
//                        });
//                }
//            }
//            accessor = realAccessor;
//        }
//        return accessor;
//    },

//    '_memberChangedCallback': function () {
//        var _self = this;
//        _self.memberChanged();
//    },

//    '_deptList': null,
//    '_deptInitList': null,
//    '_memberInitList': null,
//    '_cmbDeptList': null,
//    '_cmbMemberList': null,
//    '_selectorId': null,
//    '_selectUserId': null,
//    '_selectedDeptId': null,

//    '_domInitialized': false,


//    '_getInitDeptList': function () {
//        var _self = this;
//        var deptList = _self._deptList;
//        var deptInitList = [];
//        for (var key in deptList) {
//            var name = deptList[key].DeptName;
//            var id = deptList[key].Id;

//            deptInitList.push({ 'text': name,
//                'value': id
//            });
//        }
//        return deptInitList;
//    },

//    '_bindDeptList': function () {
//        var _self = this;
//        var deptList = _self._deptInitList;

//        _self._cmbDeptList
//              .comboxBinding(deptList, 'text', 'value');
//        if (_self._selectedDeptId) {
//            _self._cmbDeptList.val(_self._selectedDeptId);
//        }
//    },

//    '_getInitMemberList': function (memberList) {
//        var _self = this;
//        var initMemberList = [];
//        for (var key in memberList) {
//            var name = memberList[key].Name;
//            var id = memberList[key].Id;

//            initMemberList.push({ 'text': name,
//                'value': id
//            });
//        }
//        return initMemberList;
//    },

//    '_bindMemberList': function () {
//        var _self = this;
//        var memberDropDownList = _self._memberInitList;

//        _self._cmbMemberList
//                 .comboxBinding(memberDropDownList, 'text', 'value');

//        if (_self._selectUserId) {
//            _self._cmbMemberList.val(_self._selectUserId);
//        }
//        _self._memberChangedCallback();
//    },

//    'setMember': function (member) {
//        var _self = this;
//        _self._selectUserId = member.Id;
//        if (_self._domInitialized) {
//            _self._cmbMemberList.val(_self._selectUserId);
//        }
//    },

//    'setDept': function (dept) {
//        var _self = this;
//        _self._selectedDeptId = dept.Id;
//        if (_self._domInitialized) {
//            _self._cmbDeptList.val(_self._selectedDeptId);
//        }
//    },

//    'getMember': function () {
//        var _self = this;
//        var member = {};
//        member.Id = _self._cmbMemberList.val();
//        member.Name = _self._cmbMemberList.find("option:selected").text();
//        return member;
//    },
//    'setDeptList': function (deptList) {
//        var _self = this;
//        _self._deptList = deptList;
//    },

//    'memberChanged': function () {
//        var _self = this;
//        ///获得UserID
//        _self._userId = _self._cmbMemberList.val();
//    },

//    'getInitializeDom': function () {
//        var _self = this;
//        var domRoot = $("<div/>");
//        domRoot.append($('<span>部门:</span>'));

//        _self._cmbDeptList =
//                $('<select class="cmbDepartmentList" />')
//                    .css({ 'width': '100px' });
//        domRoot.append(_self._cmbDeptList);

//        domRoot.append($('<span>姓名:</span>'));

//        _self._cmbMemberList =
//                $('<select class="cmbMemberList" />')
//                    .css({ 'width': '80px' });
//        domRoot.append(_self._cmbMemberList);

//        _self._deptInitList = _self._getInitDeptList(_self._deptList);
//        _self._bindDeptList(_self._deptInitList);

//        _self._accessor().doUpdateMemberList(_self._cmbDeptList.val());

//        _self._cmbDeptList.change(function () {
//            _self._selectedDeptId = _self._cmbDeptList.val();
//            _self._accessor().doUpdateMemberList(_self._cmbDeptList.val());
//        });

//        _self._cmbMemberList.change(function () {
//            _self._selectUserId = _self._cmbMemberList.val();
//            _self.memberChanged();
//        });

//        _self._domInitialized = true;
//        return domRoot;
//    }
//};

//$.fn.memberSelector = function (memberSelector) {
//    $(this).each(function (index, item) {
//        if (this) {
//            var dom = memberSelector.getInitializeDom();
//            $(this).append(dom);
//        }
//    });
//};

